package com.sailorj.service.impl;

import com.sailorj.entity.TbPlan;
import com.sailorj.mapper.TbPlanMapper;
import com.sailorj.service.TbPlanService;
import com.sailorj.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TbPlanServiceImpl implements TbPlanService {

    @Autowired
    public TbPlanMapper planMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public void addPlan(TbPlan tbPlan) {
        planMapper.insert(tbPlan);
    }

    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    @Override
    public void updatePlanById(TbPlan tbPlan) {
        planMapper.updateByPrimaryKey(tbPlan);
    }

    @Override
    public void updatePlanSumById(int id,String num) {
        planMapper.updatePlanSumById(id,num);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public TbPlan getPlanById(int id) {
        return planMapper.selectByPrimaryKey(id);
    }

//    @Override
//    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
//    public List<TbPlan> getPlanList() {
//        return planMapper.selectPlanList();
//    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public PageBean getPlanList(Integer page, Integer size) {
        if(page!=null&&size!=null){
            page=(page-1)*size;
        }
        List<TbPlan> data = planMapper.selectPlanList(page,size);
        Long total=planMapper.getTotal();
        PageBean bean = new PageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public PageBean getPlanListOne(Integer page, Integer size) {
        if(page!=null&&size!=null){
            page=(page-1)*size;
        }
        List<TbPlan> data = planMapper.getPlanListOne(page,size);
        Long total=planMapper.getTotal();
        PageBean bean = new PageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public TbPlan findByPlanName(String name) {
        return planMapper.findPlanByName(name);
    }
}
